<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商城主页</title>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
var currentPage=${currentPage};
var totalPage = ${totalPage};
var type = "${type}";
function getList(){
	location.href="BuyerGoodsListServlet?currentPage="+currentPage+"&type="+type;
}
function getListPrice(){
	type = 1;
	getList();
}
function getListAccount(){
	type = 0;
	getList();
}
function del(id){
	var num = document.getElementById("s"+id).innerHTML;
	if(num<=1){
		alert("您不能选择少于1的数量的商品");
	}else{
		num--;
		document.getElementById("s"+id).innerHTML = num;
	}
}
function add(id,account){
	var num = document.getElementById("s"+id).innerHTML;
	if(num>=account){
		alert("您不能选择大于库存的数量的商品");
	}else{
		num++;
		document.getElementById("s"+id).innerHTML = num;
	}
}

function shop(id){
	var num = document.getElementById("s"+id).innerHTML;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState==4&&xmlhttp.status==200){
			var data = xmlhttp.responseText;
			if(data=="true"){
				alert("添加成功");
			}else{
				alert("添加失败");
			}
		}
	};
	xmlhttp.open("get","BuyerShopAddServlet?id="+id+"&num="+num,true);
	xmlhttp.send();
}
function order(id){
	var num = document.getElementById("s"+id).innerHTML;
	location.href="BuyerOrderAddServlet?id="+id+"&num="+num;
}
</script>
</head>
<body>
<jsp:include page="head.jsp"/>
<div id="container">
<table class="zebra">
<tr>
	<th>商品名称</th><th onclick="getListPrice();">价格</th><th onclick="getListAccount()">库存</th><th>店名</th><th>老板</th><th>联系方式</th><th>数量</th><th>操作</th>
</tr>
<c:forEach items="${list}" var="g">
<tr>
	<td>${g.goodsName }</td>
	<td>${g.goodsPrice }</td>
	<td>${g.goodsAccount }</td>
	<td>${g.user.userShopname}</td>
	<td>${g.user.userNickname}</td>
	<td>${g.user.userPhone}</td>
	<td>
		<c:if test="${g.goodsAccount>0}">
			<img src="../img/delete.png" onclick="del(${g.goodsId});">
			<span id="s${g.goodsId}">1</span>
			<img src="../img/add.png" onclick="add(${g.goodsId},${g.goodsAccount});">
		</c:if>
	</td>
	<td>
		<c:if test="${g.goodsAccount>0}">
			<input type="button" value="添加进购物车" onclick="shop(${g.goodsId})">
			<input type="button" value="下单" onclick="order(${g.goodsId})">
		</c:if>
	</td>
</tr>
</c:forEach>
</table>
<jsp:include page="page.jsp"/>
</div>
</body>
</html>